package com.homegoods.orderinfodetail.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.model.GoodsParamModel;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.dialog.KBaseDialogFragment;
import com.kedll.library.util.MyUtils;

import java.util.List;

import io.reactivex.Observable;

/**
 * @author QiQi
 * @ClassName: GoodParamDialog
 * @date 2018/9/23
 * @description 显示商品参数
 */
public class GoodParamDialog extends KBaseDialogFragment {
    private MultiTypeAdapter adapter;

    @Override
    public void onResume() {
        super.onResume();
//        getDialog().getWindow().setLayout(getResources().getDimensionPixelSize(R.dimen.width_60_80), getResources().getDimensionPixelSize(R.dimen.height_60_80));
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void initEvent() {
    }

    @SuppressLint("CheckResult")
    @Override
    protected void loadData(Bundle arguments) {
        List<GoodsParamModel> paramModels = arguments.getParcelableArrayList("data");
        Observable.fromIterable(paramModels)
                .map(o -> o.createItem(adapter))
                .toList()
                .subscribe(goodsParamItem -> {
                    adapter.addAll(goodsParamItem);
                    adapter.notifyDataSetChanged();
                });

    }

    @Override
    protected int getLayout() {
        return R.layout.info_dialog_param;
    }

    @Override
    protected void initView(View rootView) {
        findViewById(R.id.btn_confirm).setOnClickListener(v -> dismiss());
        RecyclerView recyclerView = findViewById(R.id.list);

        adapter = new MultiTypeAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(new DividerListItemDecoration(getContext(), DividerListItemDecoration.VERTICAL_LIST));
    }

    /**
     * 绘制虚线
     */
    public class DividerListItemDecoration extends RecyclerView.ItemDecoration {
        public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
        public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
        private Paint mPaintDash;
        private int mOrientation;
        private int height = MyUtils.dp2px(0.5f);
        private Path path;

        public DividerListItemDecoration(Context context, int orientation) {
            setOrientation(orientation);
            mPaintDash = new Paint();
            mPaintDash.setStyle(Paint.Style.STROKE);
            mPaintDash.setAntiAlias(true);
            mPaintDash.setStrokeWidth(2);
            mPaintDash.setColor(ContextCompat.getColor(context, R.color.divider_color));
            mPaintDash.setPathEffect(new DashPathEffect(new float[]{4f, 4f}, 0));
            path = new Path();

        }

        public void setOrientation(int orientation) {
            if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
                throw new IllegalArgumentException("invalid orientation");
            }
            mOrientation = orientation;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            if (mOrientation == VERTICAL_LIST) {
                drawVertical(c, parent);
            } else {
                drawHorizontal(c, parent);
            }
        }


        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (mOrientation == VERTICAL_LIST) {
                outRect.set(0, 0, 0, height);
            } else {
                outRect.set(0, 0, height, 0);
            }
        }

        private void drawVertical(Canvas c, RecyclerView parent) {
            final int left = parent.getPaddingLeft();
            final int right = parent.getWidth() - parent.getPaddingRight();

            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount - 1; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                        .getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + height;
                path.moveTo(left, top);
                path.lineTo(right, bottom);
                c.drawPath(path, mPaintDash);
            }
        }

        private void drawHorizontal(Canvas c, RecyclerView parent) {
            final int top = parent.getPaddingTop();
            final int bottom = parent.getHeight() - parent.getPaddingBottom();

            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                        .getLayoutParams();
                final int left = child.getRight() + params.rightMargin;
                final int right = left + height;
                c.drawLine(left, top, right, bottom, mPaintDash);
            }
        }
    }

}
